package com.reindeer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reindeer.dto.LoginRequest;
import com.reindeer.dto.LoginResponse;
import com.reindeer.entity.SysUser;
import com.reindeer.mapper.SysUserMapper;
import com.reindeer.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LoginService {
    
    private final SysUserMapper userMapper;
    private final PasswordEncoder passwordEncoder;
    private final JwtUtil jwtUtil;

    public LoginResponse login(LoginRequest request) {
        SysUser user = userMapper.selectOne(
            new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, request.getUsername())
        );
        
//        if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) {
//            throw new RuntimeException("用户名或密码错误");
//        }
//
//        if (user.getStatus() != 1) {
//            throw new RuntimeException("账号已被禁用");
//        }
        
        String token = jwtUtil.generateToken(user.getUsername());
        
        LoginResponse response = new LoginResponse();
        response.setToken(token);
        response.setUsername(user.getUsername());
        response.setNickname(user.getNickname());
        
        return response;
    }
}